package leetcode;

import java.util.HashSet;
import java.util.Set;

/**
 * @program: datastructureandalogorithm
 * @description:
 * @author: hmx
 * @create: 2021-10-18 17:04
 **/
public class LeetCode202 {
    public boolean isHappy(int n) {
        //用集合存储变化过程中的每个数，判断这个数是否无限循环，如果是无限循环，则说明该数一定会重复出现。
        Set<Integer> set = new HashSet<>();
        while (n != 1) {
            if (set.contains(n)) {
                return false;
            }
            set.add(n);
            int happyNum = 0;
            while (n != 0) {
                happyNum += (n % 10) * (n % 10);
                n = n / 10;
            }
            n = happyNum;

        }
        return true;
    }

    public static void main(String[] args) {
        LeetCode202 code = new LeetCode202();
        code.isHappy(19);
    }
}
